const express = require('express');
const router = express.Router();
const Brand = require('../../models/brand');

// 获取品牌列表
router.get('/list', async (req, res) => {
  try {
    const { category } = req.query;
    const query = { status: true };

    if (category) {
      query.categories = { $in: [category] };
    }

    const brands = await Brand.find(query)
      .populate('categories')
      .sort({ sort: 1, createdAt: -1 });

    res.status(200).json({
      code: 200,
      data: brands
    });
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: error.message
    });
  }
});

// 获取品牌详情
router.get('/:id', async (req, res) => {
  try {
    const brand = await Brand.findById(req.params.id)
      .populate('categories');

    if (!brand) {
      return res.status(404).json({
        code: 404,
        message: '品牌不存在'
      });
    }

    res.status(200).json({
      code: 200,
      data: brand
    });
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: error.message
    });
  }
});

module.exports = router; 